package com.ml.zookeeper.udline;

import org.apache.zookeeper.*;

import java.io.IOException;

/**
 * 服务器动态上下线示例
 * 服务端注册模拟
 */
public class DistributeServer {

    private String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
    private int sessionTimeout = 2000;
    private ZooKeeper zk;

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        DistributeServer server = new DistributeServer();
        // 1 获取zk连接
        server.getConnect();
        // 2 注册服务器到zk集群
        server.register(args[0]);
        // 3 启动业务逻辑（睡觉）
        server.business();
    }

    private void getConnect() throws IOException {
        zk = new ZooKeeper(connectString, sessionTimeout, watchedEvent -> {
        });
    }

    private void register(String hostname) throws KeeperException, InterruptedException {
        String create = zk.create("/servers/" + hostname, hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        System.out.println(create + " is online");
    }

    private void business() throws InterruptedException {
        Thread.sleep(Long.MAX_VALUE);
    }
}
